# Maintainer: Konstantin Gizdov < arch at kge dot pw >
# Contributor: Joshua Ellis < josh at jpellis dot me >
# Contributor: Stefano Campanella < stefanocampanella1729 at gmail dot com >

pkgbase=pythia8
pkgname=('pythia8' 'python-pythia8')
pkgver=8.3.09
_pkgver="${pkgver//./}"
_pkgid="${pkgbase:0:-1}${_pkgver}"
_pkgpre="${_pkgver:0:2}"
pkgrel=2
pkgdesc="High-energy physics events generator"
arch=('loong64' 'x86_64')
url="https://pythia.org"
license=('GPL2')
makedepends=('fastjet' 'hepmc' 'hepmc2' 'lhapdf' 'openmp' 'python' 'root' 'zlib')
source=("https://pythia.org/download/pythia${_pkgpre}/${_pkgid}.tgz"
        'pythia8.sh'
        'pythia8-python-3.11.patch'
        'fix_python_lib_paths.patch')
sha256sums=('5bdafd9f2c4a1c47fd8a4e82fb9f0d8fcfba4de1003b8e14be4e0347436d6c33'
            '4e373b685960e410024b4e33e22c2dea360dfedd7962837087332f428c974ae5'
            '48a4ed4be91a4ffbf52e4609b8226f8441cb6c1d75e951d4e083e3b8f1835a83'
            '661d0d54bea576bccd0075cb1f7a0cbbf7df05b241d97eec6f7fbc1389b5f992')
get_pyver () {
    python -c 'import sys; print(str(sys.version_info[0]) + "." + str(sys.version_info[1]))'
}

prepare() {
    cd "${_pkgid}"
    patch -Np1 -i "../fix_python_lib_paths.patch"
    # upstream seems to not care about py311 support: https://gitlab.com/Pythia8/releases/-/issues/128
    patch -Np1 -i "../pythia8-python-3.11.patch"
    # patch all date calls to make pythia reproducible
    sed 's/`date`/`TZ=UTC LANG=C date --date=@$SOURCE_DATE_EPOCH`/' -i configure
}

build() {
    _bin=/usr/bin
    _inc=/usr/include
    _lib=/usr/lib
    _share="/usr/share/${pkgbase}"
    _pyver="$(get_pyver)"

    cd "${_pkgid}"
    ./configure \
        --prefix=/usr \
        --prefix-bin="${_bin}" \
        --prefix-include="${_inc}" \
        --prefix-lib="${_lib}" \
        --prefix-share="${_share}" \
        --cxx=/usr/bin/g++ \
        --cxx-common="${CXXFLAGS} -fPIC" \
        --cxx-shared="-shared ${LDFLAGS} -ldl" \
        --with-fastjet3 \
        --with-fastjet3-include=${_inc} \
        --with-fastjet3-lib=${_lib} \
        --with-gzip \
        --with-gzip-include=${_inc} \
        --with-gzip-lib=${_lib} \
        --with-hepmc2 \
        --with-hepmc2-include=${_inc} \
        --with-hepmc2-lib=${_lib} \
        --with-hepmc3 \
        --with-hepmc3-include=${_inc} \
        --with-hepmc3-lib=${_lib} \
        --with-lhapdf5 \
        --with-lhapdf5-include=${_inc} \
        --with-lhapdf5-lib=${_lib} \
        --with-lhapdf6 \
        --with-lhapdf6-include=${_inc} \
        --with-lhapdf6-lib=${_lib} \
        --with-python \
        --with-python-include="/usr/include/python${_pyver}" \
        --with-python-lib="/usr/lib/python${_pyver}" \
        --with-root \
        --with-root-include=/usr/include/root \
        --with-root-lib=/usr/lib/root \
        --with-openmp \
        --with-openmp-include=${_inc} \
        --with-openmp-lib=${_lib}
    make
}

package_pythia8() {
    depends=(
        'bash'
        'gcc-libs'
        'glibc'
        'openmp'
        'python'
        'zlib'
    )
    optdepends=(
        'fastjet: fast jet finding in pp and e+e- collisions'
        'hepmc: storing collisions from Monte Carlo'
        'hepmc2: storing collisions from Monte Carlo (old interface)'
        'lhapdf: evaluate PDFs from discretised data files'
        'python-pythia8: for examples'
        'root: integrated examples with CERN ROOT data analysis framework'
    )

    cd "${_pkgid}"

    install -Dm755 bin/pythia8-config "${pkgdir}/usr/bin/pythia8-config"
    install -Dm644 lib/libpythia8.a "${pkgdir}/usr/lib/libpythia8.a"
    install -Dm755 lib/libpythia8.so "${pkgdir}/usr/lib/libpythia8.so"
    install -Dm755 lib/libpythia8lhapdf5.so "${pkgdir}/usr/lib/libpythia8lhapdf5.so"
    install -Dm755 lib/libpythia8lhapdf6.so "${pkgdir}/usr/lib/libpythia8lhapdf6.so"

    cp -r include "${pkgdir}/usr/"
    install -d "${pkgdir}/usr/share/${pkgbase}"
    cp -r share/Pythia8/* "${pkgdir}/usr/share/${pkgbase}/"
    cp -r examples "${pkgdir}/usr/share/${pkgbase}/"

    install -D "${srcdir}/pythia8.sh" "${pkgdir}/etc/profile.d/pythia8.sh"
}

package_python-pythia8() {
    pkgdesc="Python bindings for Pythia"
    depends=(
        'gcc-libs'
        'glibc'
        'pythia8'
    )
    replaces=('python-pythia')

    cd "${_pkgid}"
    _pyver="$(get_pyver)"

    install -Dm755 lib/pythia8.so "${pkgdir}/usr/lib/python${_pyver}/site-packages/pythia8.so"

    # recompile pycache to strip $pkgdir from embedded paths
    python -m compileall -d "/usr/lib/python${_pyver}" \
        "${pkgdir}/usr/lib/python${_pyver}"
    python -O -m compileall -d "/usr/lib/python${_pyver}" \
        "${pkgdir}/usr/lib/python${_pyver}"
}
